<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
  import * as echarts from 'echarts'
  require('./theme/default')
  import resize from '@/utils/resize'
  export default {
    name: 'index',
    components: {},
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '100%'
      },
      option: {
        type: Object,
        required: true
      },
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      option: {
        deep: true,
        handler(val) {
          if(val){
            this.$nextTick(() => {
              this.initChart()
            })
          }
        },
        // immediate: true // 允许首次绑定时执行
      }
    },
    mounted() {
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'default')
        this.setOptions(this.option)
      },
      setOptions(option) {
        this.chart.setOption(option)
        let _this = this
        this.chart.on('legendselectchanged', function(params) {
          // console.log("params====",params)
          _this.chart.setOption({
            legend:{selected:{[params.name]: true}}
          })
          _this.onShow(params)
        })
      },
      onShow(data){
        this.$emit('on-show', data);
      }
    }
  }
</script>

<style scoped>

</style>
